package io.renren.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.controller.AbstractController;
import io.renren.dao.SalaryWelfareManagementDao;
import io.renren.entity.SalaryWelfareManagementEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import io.renren.dao.JobChangeManagementDao;
import io.renren.entity.JobChangeManagementEntity;
import io.renren.service.JobChangeManagementService;



@Service("jobChangeManagementService")
public class JobChangeManagementServiceImpl extends AbstractController implements JobChangeManagementService {
	@Autowired
	private JobChangeManagementDao jobChangeManagementDao;

	@Autowired
	private SalaryWelfareManagementDao salaryWelfareManagementDao;
	
	@Override
	public JobChangeManagementEntity queryObject(Integer id){
		return jobChangeManagementDao.queryObject(id);
	}
	
	@Override
	public List<JobChangeManagementEntity> queryList(Map<String, Object> map){
		return jobChangeManagementDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return jobChangeManagementDao.queryTotal(map);
	}
	
	@Override
	public void save(JobChangeManagementEntity jobChangeManagement){
		jobChangeManagementDao.save(jobChangeManagement);
	}

	@Override
	public void update(JobChangeManagementEntity jobChangeManagement){
		jobChangeManagementDao.update(jobChangeManagement);
	}
	
	@Override
	public void delete(Integer id){
		jobChangeManagementDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		jobChangeManagementDao.deleteBatch(ids);
	}

	@Override
	public void approve(JSONObject params){
		String id=params.get("id").toString();
		String type=params.get("type").toString();
		JobChangeManagementEntity pojo=queryObject(new Integer(id) );
		if("fir".equals(type)){
			pojo.setFirApprover(getUser().getUsername());
			pojo.setFirApprovalDate(new Date());
		}else
		if("sec".equals(type)){
			pojo.setSecApprover(getUser().getUsername());
			pojo.setSecApprovalDate(new Date());
		} else{
			pojo.setThiApprover(getUser().getUsername());
			pojo.setThiApprovalDate(new Date());

			List<SalaryWelfareManagementEntity> list=salaryWelfareManagementDao.queryListByIdno(pojo.getIdno());
			for (SalaryWelfareManagementEntity salary : list) {
				salary.setCompany(pojo.getPresentUnit());
				salary.setCompanyId(pojo.getPresentUnitId());
				salary.setDepartment(pojo.getPresentDepartment());
				salary.setPost(pojo.getPresentPost());
				salary.setPostId(pojo.getPresentPostId());
				salary.setPostLevel(pojo.getPresentPostLevel());
				salary.setBaseSalary(pojo.getPresentBaseSalary());
				salary.setAchievementsSalary(pojo.getPresentAchievementsSalary());
				salary.setOvertimePay(pojo.getPresentOvertimePay());
				salaryWelfareManagementDao.update(salary);
			}


		}
		jobChangeManagementDao.update(pojo);

	}
	
}
